/*
 * Project Name:hbm-be.
 * File Name:CsSmOrderTypeServiceImpl.java
 * Package Name:com.hginfo.cs.be.service.impl.cs
 * Date:2018年04月24日 下午3:58:49
 * Copyright (c) 2016, hongguaninfo.com All Rights Reserved.
 *
 */
package cn.uce.cc.srv.biz.impl;

import cn.uce.base.ccos.ComplaintBigTypeVo;
import cn.uce.base.ccos.DirectionaryVo;
import cn.uce.base.ccos.LittleBigTypeVo;
import cn.uce.cc.api.service.biz.ICcosWaybillBiz;
import cn.uce.cc.api.service.biz.ISmOrderTypeBiz;
import cn.uce.cc.srv.client.JsonHttpClient;
import cn.uce.common.constants.BaseConstant;
import cn.uce.common.constants.CcConstants;
import cn.uce.common.constants.RedisKeyConstant;
import cn.uce.common.utils.RedisUtils;
import cn.uce.ics.base.vo.cs.CsSmOrderTypeVo;
import cn.uce.service.biz.ICcSmOrderTypeBiz;
import cn.uce.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工单类型表: cs_sm_order_type. <br />
 * service impl 层 <br />
 * Date: 2018年04月24日 下午3:58:49 <br />
 *
 * @author you
 * @since V1.0.0
 */
@Service("csSmOrderTypeBiz")
@Transactional(readOnly = false,rollbackFor = Exception.class)
public class CcSmOrderTypeBiz implements ISmOrderTypeBiz {
	private static final Logger log = LoggerFactory.getLogger(CcSmsMessageBiz.class);
	@Resource
	ICcSmOrderTypeBiz ccSmOrderTypeBiz;

    @Value("${spring.serviceName.queryDictionaryValue}")
    private String serviceName;
	@Resource
	RedisUtils redisUtils;

	@Resource
	private ICcosWaybillBiz ccosWaybillBiz;


	@Override
	public List<CsSmOrderTypeVo> getCsOrderType(CsSmOrderTypeVo csSmOrderTypeVo) {
		List<CsSmOrderTypeVo> returnList = null;
		try {
			returnList =ccSmOrderTypeBiz.getCsOrderType(csSmOrderTypeVo);
		} catch (Exception e) {
			log.error("csSmOrderTypeBiz:getCsOrderType",e);
		}
		return returnList;
	}

	@Override
	public List<DirectionaryVo> getCustomerType() {
		try {
			List<DirectionaryVo> dictList = null;
			if (StringUtil.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_CUSTOMER_TYPE))){
				String resultStr =queryOpenPlatFronBridge("com.ymdd.galaxy.base.api.service.IDictionaryValueApiService:queryDictionaryValue:1.0",
						new Object[]{CcConstants.WOS_CUSTOMER_TYPE,1});
				JSONObject object = JSON.parseObject(resultStr);
				String wos_responsibility_link = object.getString(CcConstants.WOS_CUSTOMER_TYPE);
				dictList = processingData(wos_responsibility_link);
				redisUtils.putString(RedisKeyConstant.DIRECTIONARY_WOS_CUSTOMER_TYPE , JSON.toJSONString(dictList) , 86400);
				return dictList;
			}else{
				String result = redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_CUSTOMER_TYPE);
				dictList = JSON.parseArray(result, DirectionaryVo.class);
				return dictList;
			}
		} catch (Exception e) {
			log.error("getCustomerType",e);
		}
		return null;
	}


	/**
	 * 工单-责任环节 wos_responsibility_link
	 * @return
	 */
	@Override
	public List<DirectionaryVo> getResponsibilityLink() {
		try {
			List<DirectionaryVo> dictList = null;
			if (StringUtil.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_RESPONSIBILITY_LINK))) {
				String resultStr = queryOpenPlatFronBridge("com.ymdd.galaxy.base.api.service.IDictionaryValueApiService:queryDictionaryValue:1.0",
						new Object[]{CcConstants.WOS_RESPONSIBILITY_LINK, 1});
				JSONObject object = JSON.parseObject(resultStr);
				String wos_responsibility_link = object.getString(CcConstants.WOS_RESPONSIBILITY_LINK);
				dictList = processingData(wos_responsibility_link);
				redisUtils.putString(RedisKeyConstant.DIRECTIONARY_WOS_RESPONSIBILITY_LINK, JSON.toJSONString(dictList), 86400);
				return dictList;
			} else {
				String result = redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_RESPONSIBILITY_LINK);
				dictList = JSON.parseArray(result, DirectionaryVo.class);
				return dictList;
			}
		} catch (Exception e) {
			log.error("getCustomerType", e);
		}
		return null;
	}

	/**
	 * 快运来电弹屏 工单-责任环节
	 * @return
	 */
	@Override
	public List<DirectionaryVo> getReCallTime() {
		try {
			List<DirectionaryVo> dictList = null;
			if (StringUtil.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_CALL_BACK_TIME_WITHIN))){
				String resultStr =queryOpenPlatFronBridge("com.ymdd.galaxy.base.api.service.IDictionaryValueApiService:queryDictionaryValue:1.0",
						new Object[]{CcConstants.WOS_CALL_BACK_TIME_WITHIN,1});
				JSONObject object = JSON.parseObject(resultStr);
				String wos_responsibility_link = object.getString(CcConstants.WOS_CALL_BACK_TIME_WITHIN);
				dictList = processingData(wos_responsibility_link);
				redisUtils.putString(RedisKeyConstant.DIRECTIONARY_WOS_CALL_BACK_TIME_WITHIN , JSON.toJSONString(dictList) , 86400);
				return  dictList;
			}else{
				String result = redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_CALL_BACK_TIME_WITHIN);
				dictList = JSON.parseArray(result, DirectionaryVo.class);
				return dictList;
			}
		} catch (Exception e) {
			log.error("getCustomerType",e);
		}
		return null;
	}

	@Override
	public List<LittleBigTypeVo> queryComplaintTypeConf(Long parentId) {
		try {
			List<LittleBigTypeVo> resultList = new ArrayList<>();
			LittleBigTypeVo order = new LittleBigTypeVo();
			order.setComplaintTypeConfId(36l);
			order.setHandleTime("0");
			order.setCauseType("下单");
			if (StringUtil.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_COMPLAINT+parentId))){
				//36为下单类
				if (parentId != null && parentId.longValue() == 36l){
					resultList.add(order);
					redisUtils.putString(RedisKeyConstant.DIRECTIONARY_COMPLAINT+parentId ,JSON.toJSONString(resultList) , 86400);
					return resultList;
				}else{
					ComplaintBigTypeVo complaintBigTypeVo = new ComplaintBigTypeVo();
					complaintBigTypeVo.setParentId(parentId);
					String resultStr =queryOpenPlatFronBridge("com.ymdd.galaxy.wos.api.service.IWorkBillApiService:queryComplaintTypeConf:1.0",
							new Object[]{complaintBigTypeVo});
					JSONObject result = JSONObject.parseObject(resultStr);
					if("true".equals(result.getString("success"))){
						resultList =  processingTypeData(result,resultList);
						if (parentId.longValue() == 0l ){
							resultList.add(order);
						}
						redisUtils.putString(RedisKeyConstant.DIRECTIONARY_COMPLAINT+parentId ,JSON.toJSONString(resultList) , 86400);
						return resultList;
					}
				}
			}else{
				String result =  redisUtils.getString(RedisKeyConstant.DIRECTIONARY_COMPLAINT+parentId);
				resultList = JSON.parseArray(result,LittleBigTypeVo.class);
				return resultList;
			}
		} catch (Exception e) {
			log.error("getCustomerType",e);
		}
		return null;
	}



	/**
	 * 查询字典
	 * @return
	 */
	public String queryOpenPlatFronBridge(String serviceName,Object[] parameters) throws IOException {
		Map<String, Object> params = new HashMap<>();
		params.put("serviceName", serviceName);
		params.put("parameters", parameters);
		return ccosWaybillBiz.queryOpenPlatFronBridge(params);
	}

	/**
	 * 处理数据
	 * @param data
	 * @return
	 */
	public List<DirectionaryVo>  processingData(String data) {
		if (StringUtil.isNotBlank(data)) {
			List<DirectionaryVo> list = new ArrayList<>();
			String r1 = data.replace("{", "");
			String r2 = r1.replace("}", "");
			String[] split = r2.split(",");
			for (String sp : split) {
				DirectionaryVo vo = new DirectionaryVo();
				String[] split1 = sp.split(":");
				vo.setKey(split1[0].replace("\"",""));
				vo.setValue(split1[1].replace("\"",""));
				list.add(vo);
			}
			return list;
		}
		return null;
	}


	/**
	 * 处理大小类数据
	 * 排除其他类数据
	 * @param data
	 * @param resultList
	 * @return
	 */
	public List<LittleBigTypeVo> processingTypeData(JSONObject data, List<LittleBigTypeVo> resultList) {
		String dataList = data.getString("data");
		List<LittleBigTypeVo> littleBigTypeVos = JSON.parseArray(dataList, LittleBigTypeVo.class);
		if (littleBigTypeVos != null && !littleBigTypeVos.isEmpty()){
			for (LittleBigTypeVo littleBigTypeVo : littleBigTypeVos) {
				if (!"其他类".equals(littleBigTypeVo.getCauseType())){
					resultList.add(littleBigTypeVo);
				}
			}
		}
		return resultList;
	}

    public List<DirectionaryVo> queryWorkOrderStatus() {
        try {
			List<DirectionaryVo> dictList = null;
            if (StringUtils.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_BILL_STATUS))) {
                Map<String, Object> params = new HashMap<>();
                params.put("serviceName", serviceName);
                params.put("parameters", new Object[]{BaseConstant.WOS_BILL_STATUS,1});
                String s = queryOpenPlatFronBridge(serviceName,new Object[]{BaseConstant.WOS_BILL_STATUS,1});
                JSONObject object = JSON.parseObject(s);
                String wos_customer_type = object.getString(BaseConstant.WOS_BILL_STATUS);
				dictList = processingData(wos_customer_type);
                redisUtils.putString(RedisKeyConstant.DIRECTIONARY_WOS_BILL_STATUS , JSON.toJSONString(dictList) , 86400);
                return dictList;
            } else {
				String result = redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_BILL_STATUS);
				dictList = JSON.parseArray(result, DirectionaryVo.class);
				return dictList;
            }
        }catch (Exception e){
            log.error("获取工单类型异常",e);
        }
        return null;
    }

	@Override
	public List<DirectionaryVo> queryWosDealType() {
		try{
			List<DirectionaryVo> dictList = null;
			if (StringUtils.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_DEAL_TYPE))) {
				String s = queryOpenPlatFronBridge(serviceName,new Object[]{BaseConstant.WOS_DEAL_TYPE,1});
				JSONObject object = JSON.parseObject(s);
				String wos_customer_type = object.getString(BaseConstant.WOS_DEAL_TYPE);
				dictList = processingData(wos_customer_type);
				redisUtils.putString(RedisKeyConstant.DIRECTIONARY_WOS_DEAL_TYPE , JSON.toJSONString(dictList) , 86400);
				return dictList;
			} else {
				String result = redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_DEAL_TYPE);
				dictList = JSON.parseArray(result, DirectionaryVo.class);
				return dictList;
			}
		}catch (Exception e){
			log.error("查询异常",e);
		}
		return null;
	}

	@Override
	public List<DirectionaryVo> queryWosFlowType() {
		try{
			List<DirectionaryVo> dictList = null;
			if (StringUtils.isBlank(redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_FLOW_TYPE))) {
				String s = queryOpenPlatFronBridge(serviceName,new Object[]{BaseConstant.WOS_FLOW_TYPE,1});
				JSONObject object = JSON.parseObject(s);
				String wos_customer_type = object.getString(BaseConstant.WOS_FLOW_TYPE);
				dictList = processingData(wos_customer_type);
				redisUtils.putString(RedisKeyConstant.DIRECTIONARY_WOS_FLOW_TYPE , JSON.toJSONString(dictList) , 86400);
				return dictList;
			} else {
				String result = redisUtils.getString(RedisKeyConstant.DIRECTIONARY_WOS_FLOW_TYPE);
				dictList = JSON.parseArray(result, DirectionaryVo.class);
				return dictList;
			}
		}catch (Exception e){
			log.error("查询异常",e);
		}
		return null;
	}
}
